package com.fz.song.look.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fz.song.look.util.RedisLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * redis分布式锁控制器
 *
 * @author gourd
 * @since 2019-07-30
 */
@RestController
@RequestMapping("/rs")
@Slf4j
public class RedissonLockController {

    String lockKey = "songfazhun";

    /**
     * 模拟并发测试加锁和不加锁
     *
     * @return
     */
    @GetMapping("/test")
    public void lock() {

        for (int i = 0; i < 1000; i++) {
            try {
                RedisLockUtil.lock(lockKey);
                System.out.println("start = " + DateUtil.now());
                Thread.sleep(2000);
                System.out.println("end = " + DateUtil.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                RedisLockUtil.unlock(lockKey);
            }

        }


    }


}